# -*- coding: utf-8 -*-
import thread
import time
from BaseDonnees import *
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import threading
import time
from datetime import datetime, timedelta
class Serveur(object):
    
        
    def __init__(self):                          #10.57.70.69,11501
        self.server= SimpleXMLRPCServer(("localhost", 8000),allow_none=True,\
                                            requestHandler=SimpleXMLRPCRequestHandler)
        self.server.register_introspection_functions()
        self.BaseDonnees = CommunicationBaseDonnees()
        Message = self.BaseDonnees.DemarrerConnection("b52equip1", "var2", "titan")
        print Message
        self.BaseDonnees.VerifierBD()
        if Message[0] == 1:
            self.EnregistrerFonction(self.BaseDonnees)
            self.Demarrer()
        
    
    def EnregistrerFonction(self,Classe):
        self.server.register_instance(Classe)
    
    def Demarrer(self):
        print self.server.system_listMethods()
        print 'OK, serveur demarre a '+time.ctime(time.time())
        self.server.serve_forever()
    
    

serveur=Serveur()
print "time", time.time()
print int((str(time.time()).split(".")[0]))
print serveur.BaseDonnees.ObtenirDeclencheur("tous les utilisateurs")
#then = datetime.now()
#while(True):
#    if(datetime.now() - then > timedelta (seconds = 10)):
#        print "ok"
#dec =  serveur.BaseDonnees.ObtenirDeclencheur("tous les utilisateurs")
#print "dec", dec

#print serveur.BaseDonnees.ObtenirContraintes('S_EMP')
#where = {'valeur_gauche':'ID_CONTRAINTES', 'valeur_droite':Contrainte['ID_CONTRAINTES']}
#self.parent.serveur.EffacerEnregistrement('PERP_CONTRAINTES', [where])
#Reponse = serveur.BaseDonnees.ObtenirEnregistrement('DECLENCHEUR', ['TYPE', 'INTERVALLE', 'DERNIEREFOIS', 'CONTRAINTEID', 'RAPPORTNAME', 'AVERTISSEMENT'])
#print Reponse
#Reponse = serveur.BaseDonnees.ObtenirEnregistrement('CONTRAINTEDECLENCHEUR', ['MESSAGE','TABLEGAUCHE','CHAMPGAUCHE',  'OPERATEUR', 'TABLEDROITE' , 'ID'])
#print Reponse
#Contrainte =  Reponse[1][0]
#print "contrainte", Contrainte
#where = " ID = " + "116"
#serveur.BaseDonnees.MettreAJour('DECLENCHEUR', Contrainte, where)
#Contrainte = {'OPERANDE_DROITE': '', 'OPERANDE_GAUCHE': 'S_DEPT', 'MESSAGE_GUIDE': '', 'ATTRIBUT_DROITE': '', 'ATTRIBUT_GAUCHE': '', 'MESSAGE_ERREUR': '', 'COMPARAISON': '', 'ID_CONTRAINTES': 4}
#serveur.BaseDonnees.InsererEnregistrement('PERP_CONTRAINTES', Contrainte)
#Contrainte = serveur.BaseDonnees.ObtenirContraintes('S_EMP')[1][0]
#print "ajouter" ,Contrainte
#Emp = {}
#Emp['ID'] = '2'
#serveur.BaseDonnees.InsererEnregistrement('PERP_CONTRAINTES', Emp)
#Reponse = serveur.BaseDonnees.ObtenirEnregistrement('PERP_PROFILS', ['NOM'])
#print Reponse[1]
#Requete = "select S_WAREHOUSE.MANAGER_ID, S_WAREHOUSE.PHONE, S_WAREHOUSE.ZIP_CODE, S_WAREHOUSE.COUNTRY, S_WAREHOUSE.STATE, S_WAREHOUSE.CITY, S_WAREHOUSE.ADDRESS, S_WAREHOUSE.REGION_ID, S_WAREHOUSE.ID, S_REGION.NAME, S_REGION.ID from S_WAREHOUSE, S_REGION "
#Reponse = serveur.BaseDonnees.ExecuterRequeteSQLWhere(Requete)
#where = " ID_CONTRAINTES = " + Contrainte['ID_CONTRAINTES']
#serveur.BaseDonnees.MettreAJour('PERP_CONTRAINTES', Contrainte, where)